package com.example.thirdhomework

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class FifthActivity : AppCompatActivity() {
    var second = 0
    var running = false
    var isrun = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d("life cycle","onCreate")
        enableEdgeToEdge()
        setContentView(R.layout.activity_fifth)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        if(savedInstanceState != null){
            second = savedInstanceState.getInt("second")
            running = savedInstanceState.getBoolean("running")
            isrun = savedInstanceState.getBoolean("isrun")
        }
        runtimer()

        val start : Button = findViewById(R.id.button_start)
        start.setOnClickListener{
            running = true
        }
        val stop : Button = findViewById(R.id.button_stop)
        stop.setOnClickListener{
            running = false
        }
        val restart : Button = findViewById(R.id.button_restart)
        restart.setOnClickListener{
            running = true
            second = 0
        }
    }

    fun runtimer(){
        val handler = Handler()
        val runnable = object : Runnable{
            @SuppressLint("DefaultLocale")
            override fun run(){
                val hours = second / 3600
                val minutes = second %3600 / 60
                val secs = second % 60
                val text : TextView = findViewById(R.id.textView2)
                text.text = String.format("%02d:%02d:%02d",hours,minutes,secs)
                if(running){
                    second ++
                }
                handler.postDelayed(this,1000)
            }
        }
        handler.post(runnable)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d("life cycle","onSaveInstanceState")
        outState.putInt("second",second)
        outState.putBoolean("running",running)
        outState.putBoolean("isrun",isrun)
    }

    override fun onDestroy(){
        super.onDestroy()
        Log.d("life cycle","onDestroy")
    }

    override fun onStart() {
        super.onStart()
        Log.d("life cycle","onStart")
    }

    override fun onStop() {
        super.onStop()
        Log.d("life cycle","onStop")
    }

    override fun onResume() {
        super.onResume()
        Log.d("life cycle","onResume")
        if(isrun){
            running = true
        }
    }

    override fun onPause() {
        super.onPause()
        Log.d("life cycle","onPause")
        isrun = running
        running = false
    }
}